package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.Result.Result;
import com.example.domain.Product;
import com.example.page.Pages;
import com.example.service.ProductService;
import com.example.utils.AliOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/product")
public class productController {

    @Autowired
    private ProductService productService;

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @GetMapping
    private List<Product> product (String productName) {
        LambdaQueryWrapper<Product> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeLambdaQueryWrapper.like(StringUtils.isNotEmpty(productName),Product::getProductName,productName);
        List<Product> list = productService.list(employeeLambdaQueryWrapper);
        return list;
    }

    @GetMapping("/{id}")
        public Result<Product> ProductById (@PathVariable Long id) {
        System.out.println(id);
        Product product = productService.getById(id);
        return Result.success(product);
    }

    @GetMapping("/page")
    public Result<Page> getAll(int page, int pageSize, String productName) {
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Product> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeLambdaQueryWrapper.like(StringUtils.isNotEmpty(productName),Product::getProductName,productName);
        employeeLambdaQueryWrapper.orderByDesc(Product::getUpdateTime);
//        分页查询
        productService.page(pageInfo, employeeLambdaQueryWrapper);
        return Result.success(pageInfo);
//        return null;
    }

    @DeleteMapping("/{productId}")
    public Result<String> delete (@PathVariable Integer productId) {
        Product product = productService.getById(productId);
        if (product.getValid() == 1) {
            return Result.error("该商品已上架，无法删除");
        } else {
            productService.removeById(productId);
            return Result.success("商品删除成功");
        }
    }

    @PutMapping
    public Result<String> update (@RequestBody Product product) {
        productService.updateById(product);
        return  Result.success("商品修改成功");
    }

    @PostMapping
    public Result<String> add (@RequestBody Product product) {
        productService.save(product);
        return  Result.success("商品添加成功");
    }

    @GetMapping("/up")
    public Result<String> UpAndDown (int valid, Long productId) {
        System.out.println(valid);
        Product product = productService.getById(productId);
        product.setValid(valid);
        productService.updateById(product);
        return Result.success("操作成功");
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {

        //调用阿里云OSS工具类进行文件上传
        String url = aliOSSUtils.upload(file);

        return Result.success(url);
    }
}


